package com.kofigyan.stateprogressbarsample;

import com.kofigyan.stateprogressbar.StateProgressBar;
import com.kofigyan.stateprogressbarsample.not_stateprogressbar.pojo.MenuItem;
import com.kofigyan.stateprogressbarsample.not_stateprogressbar.utils.ColorUtils;
import com.kofigyan.stateprogressbarsample.not_stateprogressbar.custom.ActionBar;
import com.kofigyan.stateprogressbarsample.not_stateprogressbar.custom.TypePopupWindow;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseAbility extends FractionAbility {

    protected StateProgressBar stateProgressBar;
    protected ActionBar actionBar;
    protected List<MenuItem> types = new ArrayList<>();

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_base);
        ((ComponentContainer) findComponentById(ResourceTable.Id_fl_content))
                .addComponent(LayoutScatter.getInstance(this).parse(getLayoutId(), null, false));
        initMenuData();
        findView();
    }

    protected void initMenuData() {
        types.add(new MenuItem(0, "Change Color"));
        types.add(new MenuItem(1, "Change Size"));
        types.add(new MenuItem(2, "Animate to Current State"));
        types.add(new MenuItem(3, "Change Line Thickness"));
        types.add(new MenuItem(4, "Current State"));
        types.add(new MenuItem(5, "Max State"));
        types.add(new MenuItem(6, "Check State Completed"));
        types.add(new MenuItem(7, "Enable All Completed"));
    }

    private void findView() {
        actionBar = (ActionBar) findComponentById(ResourceTable.Id_actionbar);
        actionBar.setLeftTextColor(Color.BLACK.getValue());
        actionBar.setCenterText("StateProgressBar Example");
        actionBar.setLeftIcon(ResourceTable.Media_ic_launcher, null);
        actionBar.setRightIcon(ResourceTable.Media_ic_menu, new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                new TypePopupWindow(getContext(), actionBar.getRightView())
                        .setData(types)
                        .setListener(new TypePopupWindow.OnProjectTypeClickListener() {
                            @Override
                            public void onProjectTypeClick(MenuItem menuItem) {
                                onOptionsItemSelected(menuItem);
                            }
                        })
                        .show(component);
            }
        });

        stateProgressBar =
                (StateProgressBar) findComponentById(ResourceTable.Id_state_progress_bar);
    }

    /**
     * 返回页面layout
     *
     * @return layout
     */
    protected abstract int getLayoutId();

    public boolean onOptionsItemSelected(MenuItem item) {

        new ToastDialog(getContext())
                .setText(item.getName())
                .show();

        if (stateProgressBar == null) {
            return false;
        }

        switch (item.getId()) {
            case 0:

                stateProgressBar.setForegroundColor(ColorUtils.colorResToColor(getContext(),
                        ResourceTable.Color_demo_state_foreground_color).getValue());
                stateProgressBar.setBackgroundColor(ColorUtils.colorResToColor(getContext(),
                        ResourceTable.Color_darker_gray).getValue());

                stateProgressBar.setStateNumberForegroundColor(
                        ColorUtils.colorResToColor(getContext(),
                        ResourceTable.Color_white).getValue());
                stateProgressBar.setStateNumberBackgroundColor(
                        ColorUtils.colorResToColor(getContext(),
                        ResourceTable.Color_background_dark).getValue());

                break;

            case 1:

                stateProgressBar.setStateSize(40f);
                stateProgressBar.setStateNumberTextSize(20f);

                break;

            case 2:

                stateProgressBar.enableAnimationToCurrentState(true);

                break;

            case 3:

                stateProgressBar.setStateLineThickness(10f);

                break;

            case 4:
                if (stateProgressBar.getMaxStateNumber() >=
                        StateProgressBar.StateNumber.TWO.getValue()) {
                    stateProgressBar.setCurrentStateNumber(StateProgressBar.StateNumber.TWO);
                }
                else {
                    new ToastDialog(getContext())
                            .setText("Maximum number cannot be less than current state number")
                            .show();
                }
                break;

            case 5:
                if (stateProgressBar.getCurrentStateNumber() <= StateProgressBar.StateNumber.FOUR.getValue()) {
                    stateProgressBar.setMaxStateNumber(StateProgressBar.StateNumber.FOUR);
                } else {
                    new ToastDialog(getContext())
                            .setText("Maximum number cannot be less than current state number")
                            .show();
                }
                break;

            case 6:

                stateProgressBar.checkStateCompleted(Boolean.TRUE);

                break;


            case 7:

                stateProgressBar.setAllStatesCompleted(Boolean.TRUE);

                break;
        }

        return true;
    }

}
